<template>
  <div class="app-container normalFormBg">
    <div class="dataManage bgFF">
      <div class="dataManageTop pageFlex pageSpaceBetween">
        <div class="itemBox" :class="{'active': step >= 1}" @click="handleStep(1)">
          填写基本信息
          <div class="sanBox"></div>
        </div>
        <div class="itemBox" :class="{'active': step >= 2}" @click="handleStep(2)">
          商品细节介绍
          <div class="whiteSanBox"></div>
          <div class="sanBox"></div>
        </div>
        <div class="itemBox" :class="{'active': step >= 3}" @click="handleStep(3)">
          放置商品
          <div class="whiteSanBox"></div>
          <div class="sanBox"></div>
        </div>
        <div class="itemBox" :class="{'active': step >= 4}" @click="handleStep(4)">
          完成添加
          <div class="whiteSanBox"></div>
        </div>
      </div>
      <el-form ref="formRef" class="dataManageContent" :model="form" :rules="rules" label-width="200" style="margin-top: 24px;">
        <div class="normalBox" v-if="step === 1">
          <div class="titleBox pageFlex pageAlignCenter">
            <div class="line"></div>
            <div class="title">必填信息请确认信息无误</div>
          </div>
          <div class="contentBox bgFF">
            <el-form-item label="商品图片" prop="cover">
              <div class="imgBox">
                <ImageUpload ref="cover" v-model="form.cover" bg="#FFFFFF"></ImageUpload>
              </div>
            </el-form-item>
            <el-form-item label="商品名称" prop="name">
              <el-input
                  class="inputBox"
                  v-model="form.name"
                  placeholder="请输入商品名称"
                  clearable
              />
            </el-form-item>
            <el-form-item label="关键字" prop="keywords">
              <el-input
                  class="inputBox"
                  v-model="form.keywords"
                  placeholder="请输入关键字"
                  clearable
              />
            </el-form-item>
            <el-form-item label="商品类型" prop="type">
              <SelectOption class="selectOptionBox" ref="typeSelectOption" placeHolder="请选择类型" title="类型选择"
                v-model="form.type" :defaultFormValue="typeDefault" @loadOptions="typeDataLoad" @chooseError="chooseTypeError"/>
            </el-form-item>
            <el-form-item label="商品条码" prop="barCode">
              <el-input
                  class="inputBox"
                  v-model="form.barCode"
                  placeholder="请输入商品条码"
                  clearable
              />
            </el-form-item>
            <el-form-item label="成本价格" prop="costPrice">
              <el-input
                  class="inputBox"
                  v-model="form.costPrice"
                  placeholder="请输入成本价格"
                  clearable
                  type="number"
              />
            </el-form-item>
            <el-form-item label="价格" prop="price">
              <el-input
                  class="inputBox"
                  v-model="form.price"
                  placeholder="请输入价格"
                  clearable
                  type="number"
              />
            </el-form-item>
            <el-form-item label="供应商" prop="manufacturerId">
              <div class="valueBox pageFlex pageAlignCenter" @click="showTableModal('manufacturerId')">
                <el-text class="value">{{ form.manufacturerId || '请选择供应商' }}</el-text>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </el-form-item>
            <el-form-item label="商品品牌" prop="brandId">
              <div class="valueBox pageFlex pageAlignCenter" @click="showTableModal('brandId')">
                <el-text class="value">{{ form.brandId || '请选择品牌' }}</el-text>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </el-form-item>
            <el-form-item label="规格" prop="spec">
              <el-input
                  class="inputBox"
                  v-model="form.spec"
                  placeholder="请输入规格"
                  clearable
              />
            </el-form-item>
            <el-form-item label="单位" prop="unit">
              <el-input
                  class="inputBox"
                  v-model="form.unit"
                  placeholder="请输入单位"
                  clearable
              />
            </el-form-item>
            <!-- <el-form-item label="库存数量" prop="stock">
              <el-input-number class="inputBox" v-model="form.stock"/>
            </el-form-item> -->
            <el-form-item label="存储条件" prop="storageCondition">
              <el-input
                  class="textareaBox"
                  v-model="form.storageCondition"
                  type="textarea"
                  :rows="6"
                  placeholder="请输入存储条件"
              />
            </el-form-item>
          </div>
          <div class="line"></div>
          <div class="dataManageBottom pageFlex pageFlexCenter">
            <el-button type="primary" @click="handleStep()">下一步</el-button>
          </div>
        </div>
        <div class="normalBox" v-if="step === 2">
          <div class="titleBox pageFlex pageAlignCenter">
            <div class="line"></div>
            <div class="title">请设置商品细节介绍信息</div>
          </div>
          <div class="contentBox bgFF pageFlexColumn pageAlignCenter">
            <el-form-item label="" prop="content" label-position="top" style="width: 788px;">
              <editor v-model="form.content" :min-height="500"/>
            </el-form-item>
          </div>
          <div class="line"></div>
          <div class="dataManageBottom pageFlex pageFlexCenter">
            <el-button type="primary" @click="handleStep(1)">上一步</el-button>
            <el-button type="primary" @click="handleStep()">下一步</el-button>
          </div>
        </div>
        <div class="normalBox" v-if="step === 3">
          <div class="titleBox pageFlex pageAlignCenter">
            <div class="line"></div>
            <div class="title">选择仓库、货架、货位放置商品</div>
          </div>
          <div class="contentBox bgFF">
            <el-form-item label="仓库" prop="warehouseId">
              <div class="valueBox pageFlex pageAlignCenter" @click="showTableModal('warehouseId')">
                <el-text class="value">{{ form.warehouseId || '请选择仓库' }}</el-text>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </el-form-item>
            <el-form-item label="货架" prop="shelfId">
              <div class="valueBox pageFlex pageAlignCenter" @click="showTableModal('shelfId')">
                <el-text class="value">{{ form.shelfId || '请选择货架' }}</el-text>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </el-form-item>
            <el-form-item label="货位" prop="locationId">
              <div class="valueBox pageFlex pageAlignCenter" @click="showTableModal('locationId')">
                <el-text class="value">{{ form.locationId || '请选择货位' }}</el-text>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </el-form-item>
          </div>
          <div class="line"></div>
          <div class="dataManageBottom pageFlex pageFlexCenter">
            <el-button type="primary" @click="handleStep(2)">上一步</el-button>
            <el-button type="primary" @click="handleStep()">下一步</el-button>
          </div>
        </div>
        <div class="normalBox" v-if="step === 4">
          <div class="titleBox pageFlex pageAlignCenter">
            <div class="line"></div>
            <div class="title">确认并添加商品</div>
          </div>
          <div class="contentBox bgFF">
            <el-result
                icon="success"
                title="提示"
                sub-title="请确认前面步骤的内容都填写完毕且信息无误后，点击下方提交按钮进行商品添加"
            >
              <template #extra>
                <div class="dataManageBottom pageFlex pageFlexCenter">
                  <el-button type="primary" @click="handleStep(3)">上一步</el-button>
                  <el-button type="primary" :loading="submitLoading" @click="submitNow">提交</el-button>
                </div>
              </template>
            </el-result>
          </div>
        </div>
      </el-form>
    </div>
    <el-dialog v-model="manufacturerShow" title="选择供货商" width="825">
      <div class="tableModalBox">
        <div class="tableModalBox_top pageFlex pageAlignCenter">
          <div class="line"></div>
          <div class="title">请进行选择</div>
        </div>
        <div class="searchBox">
          <el-form :model="manufacturerParams" ref="manufacturerTableModal" :inline="true" @submit.prevent>
            <el-form-item prop="name" style="margin: 0 6px 0 0;">
              <el-input
                  class="inputBox"
                  v-model="manufacturerParams.name"
                  placeholder="请输入供货商名称"
                  clearable
                  @keyup.enter="handleTableModalQuery()"
              />
            </el-form-item>
            <el-form-item style="margin: 0 6px 0 0;">
              <el-button class="normalFormBg searchBtn" icon="Search" @click="handleTableModalQuery()"></el-button>
              <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetTableModalQuery()"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="tableBox">
          <el-table
            ref="manufacturerTableRef"
            v-loading="manufacturerLoading"
            :data="manufacturerDataList"
            border
            height="400"
            @select="handleTableModalSelect"
            @select-all="handleTableModalAll">
            <el-table-column type="selection" width="55" :selectable="setTableModalSelectable"/>
            <el-table-column prop="id" label="企业ID" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="companyName" label="企业名称" width="200" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="thumb" label="LOGO" width="80" align="center">
              <template #default="scope">
                <div class="tableImgBox2">
                  <el-image class="img" fit="contain" lazy :src="setCoverUrl(scope.row.thumb)" v-if="scope.row.thumb" />
                  <div class="emptyBox" v-else>暂无图片</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="contacts" label="联系人" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="telephone" label="联系电话" width="150" :show-overflow-tooltip="true"></el-table-column>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
          <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
            <div class="totalText">共计 {{manufacturerTotal}} 条</div>
            <el-pagination background layout="prev, pager, next" :total="manufacturerTotal" :current-page="manufacturerParams.pageNum" :page-size="manufacturerParams.pageSize" @current-change="handleTablePageChange"/>
          </div>
        </div>
        <div class="lineBox"></div>
        <div class="bottomBox pageFlex pageFlexCenter">
          <el-button @click="clearTableModalChoose">清空当前选择</el-button>
          <el-button type="primary" :disabled="!manufacturerCur" @click="confirmTableModalChoose">确认选择</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog v-model="brandShow" title="选择品牌" width="550">
      <div class="tableModalBox">
        <div class="tableModalBox_top pageFlex pageAlignCenter">
          <div class="line"></div>
          <div class="title">请进行选择</div>
        </div>
        <div class="searchBox">
          <el-form :model="brandParams" ref="brandTableModal" :inline="true" @submit.prevent>
            <el-form-item prop="name" style="margin: 0 6px 0 0;">
              <el-input
                  class="inputBox"
                  v-model="brandParams.name"
                  placeholder="请输入品牌名称"
                  clearable
                  @keyup.enter="handleTableModalQuery()"
              />
            </el-form-item>
            <el-form-item style="margin: 0 6px 0 0;">
              <el-button class="normalFormBg searchBtn" icon="Search" @click="handleTableModalQuery()"></el-button>
              <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetTableModalQuery()"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="tableBox">
          <el-table
            ref="brandTableRef"
            v-loading="brandLoading"
            :data="brandDataList"
            border
            height="400"
            @select="handleTableModalSelect"
            @select-all="handleTableModalAll">
            <el-table-column type="selection" width="55" :selectable="setTableModalSelectable"/>
            <el-table-column prop="id" label="品牌ID" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="name" label="品牌名称" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="cover" label="品牌LOGO" width="100" align="center">
              <template #default="scope">
                <div class="tableImgBox2">
                  <el-image class="img" fit="contain" lazy :src="setCoverUrl(scope.row.cover)" v-if="scope.row.cover" />
                  <div class="emptyBox" v-else>暂无图片</div>
                </div>
              </template>
            </el-table-column>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
          <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
            <div class="totalText">共计 {{brandTotal}} 条</div>
            <el-pagination background layout="prev, pager, next" :total="brandTotal" :current-page="brandParams.pageNum" :page-size="brandParams.pageSize" @current-change="handleTablePageChange"/>
          </div>
        </div>
        <div class="lineBox"></div>
        <div class="bottomBox pageFlex pageFlexCenter">
          <el-button @click="clearTableModalChoose">清空当前选择</el-button>
          <el-button type="primary" :disabled="!brandCur" @click="confirmTableModalChoose">确认选择</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog v-model="warehouseShow" title="选择所属仓库" width="900">
      <div class="tableModalBox">
        <div class="tableModalBox_top pageFlex pageAlignCenter">
          <div class="line"></div>
          <div class="title">请进行选择</div>
        </div>
        <div class="searchBox">
          <el-form :model="warehouseParams" ref="warehouseTableModal" :inline="true" @submit.prevent>
            <el-form-item prop="name" style="margin: 0 6px 0 0;">
              <el-input
                  class="inputBox"
                  v-model="warehouseParams.name"
                  placeholder="请输入仓库名查询"
                  clearable
                  @keyup.enter="handleTableModalQuery()"
              />
            </el-form-item>
            <el-form-item style="margin: 0 6px 0 0;">
              <el-button class="normalFormBg searchBtn" icon="Search" @click="handleTableModalQuery()"></el-button>
              <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetTableModalQuery()"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="tableBox">
          <el-table
            ref="warehouseTableRef"
            v-loading="warehouseLoading"
            :data="warehouseDataList"
            border
            height="400"
            @select="handleTableModalSelect"
            @select-all="handleTableModalAll">
            <el-table-column type="selection" width="55" :selectable="setTableModalSelectable"/>
            <el-table-column prop="id" label="仓库ID" width="200" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="warehouseNo" label="仓库编码" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="name" label="仓库名称" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="cover" label="仓库图片" width="80" align="center">
              <template #default="scope">
                <div class="tableImgBox1">
                  <el-image class="img" fit="contain" lazy :src="setCoverUrl(scope.row.cover)"
                    @click="handlePreviewShow(setCoverUrl(scope.row.cover))" v-if="scope.row.cover" />
                  <div class="emptyBox" v-else>暂无图片</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="capacity" label="容量" width="150" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="所属区域" width="200" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.province}}/{{scope.row.city}}/{{scope.row.district}}</div>
              </template>
          </el-table-column>
          <el-table-column prop="address" label="地址" width="200" :show-overflow-tooltip="true"></el-table-column>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
          <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
            <div class="totalText">共计 {{warehouseTotal}} 条</div>
            <el-pagination background layout="prev, pager, next" :total="warehouseTotal" :current-page="warehouseParams.pageNum" :page-size="warehouseParams.pageSize" @current-change="handleTablePageChange"/>
          </div>
        </div>
        <div class="lineBox"></div>
        <div class="bottomBox pageFlex pageFlexCenter">
          <el-button @click="clearTableModalChoose">清空当前选择</el-button>
          <el-button type="primary" :disabled="!warehouseCur" @click="confirmTableModalChoose">确认选择</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog v-model="shelfShow" title="选择货架" width="600">
      <div class="tableModalBox">
        <div class="tableModalBox_top pageFlex pageAlignCenter">
          <div class="line"></div>
          <div class="title">请进行选择</div>
        </div>
        <div class="searchBox">
          <el-form :model="shelfParams" ref="shelfTableModal" :inline="true" @submit.prevent>
            <el-form-item prop="name" style="margin: 0 6px 0 0;">
              <el-input
                  class="inputBox"
                  v-model="shelfParams.name"
                  placeholder="请输入货架名称"
                  clearable
                  @keyup.enter="handleTableModalQuery()"
              />
            </el-form-item>
            <el-form-item style="margin: 0 6px 0 0;">
              <el-button class="normalFormBg searchBtn" icon="Search" @click="handleTableModalQuery()"></el-button>
              <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetTableModalQuery()"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="tableBox">
          <el-table
            ref="shelfTableRef"
            v-loading="shelfLoading"
            :data="shelfDataList"
            border
            height="400"
            @select="handleTableModalSelect"
            @select-all="handleTableModalAll">
            <el-table-column type="selection" width="55" :selectable="setTableModalSelectable"/>
            <el-table-column prop="id" label="货架ID" width="200" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="name" label="货架名称" width="200" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="cover" label="货架图片" width="100" align="center">
              <template #default="scope">
                <div class="tableImgBox2">
                  <el-image class="img" fit="contain" lazy :src="setCoverUrl(scope.row.cover)" v-if="scope.row.cover" />
                  <div class="emptyBox" v-else>暂无图片</div>
                </div>
              </template>
            </el-table-column>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
          <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
            <div class="totalText">共计 {{shelfTotal}} 条</div>
            <el-pagination background layout="prev, pager, next" :total="shelfTotal" :current-page="shelfParams.pageNum" :page-size="shelfParams.pageSize" @current-change="handleTablePageChange"/>
          </div>
        </div>
        <div class="lineBox"></div>
        <div class="bottomBox pageFlex pageFlexCenter">
          <el-button @click="clearTableModalChoose">清空当前选择</el-button>
          <el-button type="primary" :disabled="!shelfCur" @click="confirmTableModalChoose">确认选择</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog v-model="locationShow" title="选择货位" width="635">
      <div class="tableModalBox">
        <div class="tableModalBox_top pageFlex pageAlignCenter">
          <div class="line"></div>
          <div class="title">请进行选择</div>
        </div>
        <div class="searchBox">
          <el-form :model="locationParams" ref="locationTableModal" :inline="true" @submit.prevent>
            <el-form-item prop="name" style="margin: 0 6px 0 0;">
              <el-input
                  class="inputBox"
                  v-model="locationParams.name"
                  placeholder="请输入货位名称"
                  clearable
                  @keyup.enter="handleTableModalQuery()"
              />
            </el-form-item>
            <el-form-item style="margin: 0 6px 0 0;">
              <el-button class="normalFormBg searchBtn" icon="Search" @click="handleTableModalQuery()"></el-button>
              <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetTableModalQuery()"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="tableBox">
          <el-table
            ref="locationTableRef"
            v-loading="locationLoading"
            :data="locationDataList"
            border
            height="400"
            @select="handleTableModalSelect"
            @select-all="handleTableModalAll">
            <el-table-column type="selection" width="55" :selectable="setTableModalSelectable"/>
            <el-table-column prop="id" label="货位ID" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="locationNo" label="货位编码" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
            <el-table-column prop="name" label="货位名称" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="cover" label="货位图片" width="80" align="center">
              <template #default="scope">
                <div class="tableImgBox2">
                  <el-image class="img" fit="contain" lazy :src="setCoverUrl(scope.row.cover)" v-if="scope.row.cover" />
                  <div class="emptyBox" v-else>暂无图片</div>
                </div>
              </template>
            </el-table-column>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
          <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
            <div class="totalText">共计 {{locationTotal}} 条</div>
            <el-pagination background layout="prev, pager, next" :total="locationTotal" :current-page="locationParams.pageNum" :page-size="locationParams.pageSize" @current-change="handleTablePageChange"/>
          </div>
        </div>
        <div class="lineBox"></div>
        <div class="bottomBox pageFlex pageFlexCenter">
          <el-button @click="clearTableModalChoose">清空当前选择</el-button>
          <el-button type="primary" :disabled="!locationCur" @click="confirmTableModalChoose">确认选择</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import ImageUpload from '@/views/gasManage/components/ImageUpload';
import SelectOption from '@/views/gasManage/components/SelectOption';
import { searchCategoryApi } from '@/api/gasManage/stockManage/goodCategory';
import { searchBrandsApi } from '@/api/gasManage/stockManage/goodBrand';
import { searchWarehouseApi } from '@/api/gasManage/stockManage/storageManage';
import { searchLocationApi } from '@/api/gasManage/stockManage/locationManage';
import { searchShelvesApi } from '@/api/gasManage/stockManage/shelfManage';
import { addGoodApi } from '@/api/gasManage/stockManage/goodInfo';
import { searchManufacturerApi } from '@/api/gasManage/defaultSetting/companyInfo';
import {computed, nextTick} from 'vue';
import { ElMessageBox } from 'element-plus';
import { CircleCheck } from '@element-plus/icons-vue';
const { proxy } = getCurrentInstance();
const checkPrice = (rule, value, callback) => {
  if(form.value.costPrice !== '' && Number(value) < Number(form.value.costPrice)){
    callback(new Error('商品价格不能小于商品成本价格！'));
  }else{
    callback();
  }
}
const checkCostPrice = (rule, value, callback) => {
  if(form.value.price !== '' && Number(form.value.price) < Number(value)){
    callback(new Error('商品成本价格不能大于商品价格！'));
  }else{
    callback();
  }
}
const form = ref({
  cover: '',
  name: '',
  type: '',
  brandId: '',
  manufacturerId: '',
  barCode: '',
  content: '',
  price: '',
  costPrice: '',
  spec: '',
  unit: '',
  warehouseId: '',
  shelfId: '',
  locationId: '',
  locationNo: '',
  storageCondition: '',
});
const rules = ref({
  cover: [{ required: true, message: "商品图片未上传", trigger: "blur" }],
  name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
  type: [{ required: true, message: "请选择类型", trigger: "blur" }],
  barCode: [{ required: true, message: "商品条码不能为空", trigger: "blur" }],
  price: [
    { required: true, message: "价格不能为空", trigger: "blur" },
    {validator: checkPrice, trigger: 'blur'}
  ],
  costPrice: [
    { required: true, message: "成本价格不能为空", trigger: "blur" },
    {validator: checkCostPrice, trigger: 'blur'}
  ],
  warehouseId: [{ required: true, message: "请选择仓库", trigger: "blur" }],
  shelfId: [{ required: true, message: "请选择货架", trigger: "blur" }],
  locationId: [{ required: true, message: "请选择货位", trigger: "blur" }],
});
const submitLoading = ref(false);
const step = ref(1);
function handleStep(num){
  if(num < step.value){
    stepChange(num);
  }else{
    proxy.$refs["formRef"].validate(valid => {
      if (valid) {
        stepChange(num);
      }else{
        proxy.$modal.msgError('填写内容有误！请检查');
      }
    })
  }
}
function stepChange(num){
  if(num){
    step.value = num;
  }else{
    step.value++;
  }
}
// ------------------------------选择类别-----------------------------
const typeDefault = ref('');
async function typeDataLoad(e){
  const { node, resolve } = e;
  let id = node.level === 0 ? 0 : node.data.id;
  let data = [];
  if(node.data.isEnable != 0){
    data = await goodsCategoryApi({parentId: id});
    if(data.length){
      data.map(item=>{
        item.checked = item.isEnable == 0;
        return item
      })
    }
  }
  proxy.$refs.typeSelectOption.handleLoadApi(data, node, resolve);
}
function goodsCategoryApi(params){
  return new Promise(resolve => {
    searchCategoryApi(params).then(res => {
      resolve(Array.isArray(res.rows) ? res.rows : []);
    }).catch(() => {resolve([])})
  })
}
function chooseTypeError(e){
  proxy.$modal.msgError(e.msg);
}
// --------------------------------------表格弹窗统一处理----------------------------
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const setCoverUrl = computed(()=>{
  return url => {
    if(url){
      return baseUrl+url
    }
    return  ''
  }
})
function showTableModal(name){
  manufacturerShow.value = false;
  manufacturerCur.value = null;
  brandShow.value = false;
  brandCur.value = null;
  warehouseShow.value = false;
  warehouseCur.value = null;
  shelfShow.value = false;
  shelfCur.value = null;
  locationShow.value = false;
  locationCur.value = null;
  if(name === 'manufacturerId'){
    manufacturerShow.value = true;
    handleTableModalQuery();
    nextTick(()=>{
      proxy.$refs.manufacturerTableRef.clearSelection();
    })
  }else if(name === 'brandId'){
    brandShow.value = true;
    handleTableModalQuery();
    nextTick(()=>{
      proxy.$refs.brandTableRef.clearSelection();
    })
  }else if(name === 'warehouseId'){
    warehouseShow.value = true;
    handleTableModalQuery();
    nextTick(()=>{
      proxy.$refs.warehouseTableRef.clearSelection();
    })
  }else if(name === 'shelfId'){
    if(form.value.warehouseId){
      shelfShow.value = true;
      shelfParams.value.warehouseId = form.value.warehouseId;
      handleTableModalQuery();
      nextTick(()=>{
        proxy.$refs.shelfTableRef.clearSelection();
      })
    }else{
      proxy.$modal.msgError('请先选择仓库！');
    }
  }else if(name === 'locationId'){
    if(form.value.shelfId){
      locationShow.value = true;
      locationParams.value.shelfId = form.value.shelfId;
      handleTableModalQuery();
      nextTick(()=>{
        proxy.$refs.locationTableRef.clearSelection();
      })
    }else{
      proxy.$modal.msgError('请先选择货架！');
    }
  }
}
function handleTableModalQuery(){
  if(manufacturerShow.value){
    manufacturerParams.value.pageNum = 1;
  }else if(brandShow.value){
    brandParams.value.pageNum = 1;
  }else if(warehouseShow.value){
    warehouseParams.value.pageNum = 1;
  }else if(shelfShow.value){
    shelfParams.value.pageNum = 1;
  }else if(locationShow.value){
    locationParams.value.pageNum = 1;
  }
  handleGetTableList();
}
function handleGetTableList(){
  if(manufacturerShow.value){
    manufacturerLoading.value = true;
    searchManufacturerApi(manufacturerParams.value).then(res=>{
      manufacturerTotal.value = Number(res.total);
      manufacturerDataList.value = Array.isArray(res.rows) ? res.rows : [];
      manufacturerLoading.value = false;
      if(form.value.manufacturerId && manufacturerDataList.value.length){
        let index = manufacturerDataList.value.findIndex(item=>item.id === form.value.manufacturerId);
        if(index !== -1){
          nextTick(()=>{
            manufacturerCur.value = manufacturerDataList.value[index];
            proxy.$refs.manufacturerTableRef.toggleRowSelection(manufacturerCur.value, true);
          })
        }
      }
    }).catch(()=>{
      manufacturerLoading.value = false;
      manufacturerDataList.value = [];
    })
  }else if(brandShow.value){
    brandLoading.value = true;
    searchBrandsApi(brandParams.value).then(res=>{
      brandTotal.value = Number(res.total);
      brandDataList.value = Array.isArray(res.rows) ? res.rows : [];
      brandLoading.value = false;
      if(form.value.brandId && brandDataList.value.length){
        let index = brandDataList.value.findIndex(item=>item.id === form.value.brandId);
        if(index !== -1){
          nextTick(()=>{
            brandCur.value = brandDataList.value[index];
            proxy.$refs.brandTableRef.toggleRowSelection(brandCur.value, true);
          })
        }
      }
    }).catch(()=>{
      brandLoading.value = false;
      brandDataList.value = [];
    })
  }else if(warehouseShow.value){
    warehouseLoading.value = true;
    searchWarehouseApi(warehouseParams.value).then(res=>{
      warehouseTotal.value = Number(res.total);
      warehouseDataList.value = Array.isArray(res.rows) ? res.rows : [];
      warehouseLoading.value = false;
      if(form.value.warehouseId && warehouseDataList.value.length){
        let index = warehouseDataList.value.findIndex(item=>item.id === form.value.warehouseId);
        if(index !== -1){
          nextTick(()=>{
            warehouseCur.value = warehouseDataList.value[index];
            proxy.$refs.warehouseTableRef.toggleRowSelection(warehouseCur.value, true);
          })
        }
      }
    }).catch(()=>{
      warehouseLoading.value = false;
      warehouseDataList.value = [];
    })
  }else if(shelfShow.value){
    shelfLoading.value = true;
    searchShelvesApi(shelfParams.value).then(res=>{
      shelfTotal.value = Number(res.total);
      shelfDataList.value = Array.isArray(res.rows) ? res.rows : [];
      shelfLoading.value = false;
      if(form.value.shelfId && shelfDataList.value.length){
        let index = shelfDataList.value.findIndex(item=>item.id === form.value.shelfId);
        if(index !== -1){
          nextTick(()=>{
            shelfCur.value = shelfDataList.value[index];
            proxy.$refs.shelfTableRef.toggleRowSelection(shelfCur.value, true);
          })
        }
      }
    }).catch(()=>{
      shelfLoading.value = false;
      shelfDataList.value = [];
    })
  }else if(locationShow.value){
    locationLoading.value = true;
    searchLocationApi(locationParams.value).then(res=>{
      locationTotal.value = Number(res.total);
      locationDataList.value = Array.isArray(res.rows) ? res.rows : [];
      locationLoading.value = false;
      if(form.value.locationId && locationDataList.value.length){
        let index = locationDataList.value.findIndex(item=>item.id === form.value.locationId);
        if(index !== -1){
          nextTick(()=>{
            locationCur.value = locationDataList.value[index];
            proxy.$refs.locationTableRef.toggleRowSelection(locationCur.value, true);
          })
        }
      }
    }).catch(()=>{
      locationLoading.value = false;
      locationDataList.value = [];
    })
  }
}
function setTableModalSelectable(row){
  if(manufacturerShow.value){
    return row.validated == 3
  }else{
    return row.isEnable != 0
  }
}
function resetTableModalQuery(){
  if(manufacturerShow.value){
    proxy.resetForm("manufacturerTableModal");
    handleTableModalQuery();
  }else if(brandShow.value){
    proxy.resetForm("brandTableModal");
    handleTableModalQuery();
  }else if(warehouseShow.value){
    proxy.resetForm("warehouseTableModal");
    handleTableModalQuery();
  }else if(shelfShow.value){
    proxy.resetForm("shelfTableModal");
    handleTableModalQuery();
  }else if(locationShow.value){
    proxy.resetForm("locationTableModal");
    handleTableModalQuery();
  }
}
function handleTableModalSelect(e){
  let data = e[0];
  if(manufacturerShow.value){
    if(e.length > 1){
      data = e[1]
    }
    if(!data || (manufacturerCur.value && manufacturerCur.value.id && manufacturerCur.value.id === data.id)){
      manufacturerCur.value = null;
      return false;
    }
    let index = manufacturerDataList.value.findIndex(item=>item.id === data.id);
    proxy.$refs.manufacturerTableRef.clearSelection();
    manufacturerCur.value = data;
    proxy.$refs.manufacturerTableRef.toggleRowSelection(manufacturerDataList.value[index], true);
  }else if(brandShow.value){
    if(e.length > 1){
      data = e[1]
    }
    if(!data || (brandCur.value && brandCur.value.id && brandCur.value.id === data.id)){
      brandCur.value = null;
      return false;
    }
    let index = brandDataList.value.findIndex(item=>item.id === data.id);
    proxy.$refs.brandTableRef.clearSelection();
    brandCur.value = data;
    proxy.$refs.brandTableRef.toggleRowSelection(brandDataList.value[index], true);
  }else if(warehouseShow.value){
    if(e.length > 1){
      data = e[1]
    }
    if(!data || (warehouseCur.value && warehouseCur.value.id && warehouseCur.value.id === data.id)){
      warehouseCur.value = null;
      return false;
    }
    let index = warehouseDataList.value.findIndex(item=>item.id === data.id);
    proxy.$refs.warehouseTableRef.clearSelection();
    warehouseCur.value = data;
    proxy.$refs.warehouseTableRef.toggleRowSelection(warehouseDataList.value[index], true);
  }else if(shelfShow.value){
    if(e.length > 1){
      data = e[1]
    }
    if(!data || (shelfCur.value && shelfCur.value.id && shelfCur.value.id === data.id)){
      shelfCur.value = null;
      return false;
    }
    let index = shelfDataList.value.findIndex(item=>item.id === data.id);
    proxy.$refs.shelfTableRef.clearSelection();
    shelfCur.value = data;
    proxy.$refs.shelfTableRef.toggleRowSelection(shelfDataList.value[index], true);
  }else if(locationShow.value){
    if(e.length > 1){
      data = e[1]
    }
    if(!data || (locationCur.value && locationCur.value.id && locationCur.value.id === data.id)){
      locationCur.value = null;
      return false;
    }
    let index = locationDataList.value.findIndex(item=>item.id === data.id);
    proxy.$refs.shelfTableRef.clearSelection();
    locationCur.value = data;
    proxy.$refs.shelfTableRef.toggleRowSelection(locationDataList.value[index], true);
  }
}
function handleTableModalAll(){
  if(manufacturerShow.value){
    proxy.$refs.manufacturerTableRef.clearSelection();
    manufacturerCur.value = null;
  }else if(brandShow.value){
    proxy.$refs.brandTableRef.clearSelection();
    brandCur.value = null;
  }else if(warehouseShow.value){
    proxy.$refs.warehouseTableRef.clearSelection();
    warehouseCur.value = null;
  }else if(shelfShow.value){
    proxy.$refs.shelfTableRef.clearSelection();
    shelfCur.value = null;
  }else if(locationShow.value){
    proxy.$refs.locationTableRef.clearSelection();
    locationCur.value = null;
  }
}
function handleTablePageChange(e){
  if(manufacturerShow.value){
    manufacturerParams.value.pageNum = e;
    handleGetTableList();
  }else if(brandShow.value){
    brandParams.value.pageNum = e;
    handleGetTableList();
  }else if(warehouseShow.value){
    warehouseParams.value.pageNum = e;
    handleGetTableList();
  }else if(shelfShow.value){
    shelfParams.value.pageNum = e;
    handleGetTableList();
  }else if(locationShow.value){
    locationParams.value.pageNum = e;
    handleGetTableList();
  }
}
function clearTableModalChoose(){
  if(manufacturerShow.value){
    form.value.manufacturerId = '';
    proxy.$refs.manufacturerTableRef.clearSelection();
    hideTableModal();
  }else if(brandShow.value){
    form.value.brandId = '';
    proxy.$refs.brandTableRef.clearSelection();
    hideTableModal();
  }else if(warehouseShow.value){
    form.value.warehouseId = '';
    form.value.shelfId = '';
    form.value.locationId = '';
    proxy.$refs.warehouseTableRef.clearSelection();
    hideTableModal();
  }else if(shelfShow.value){
    form.value.shelfId = '';
    form.value.locationId = '';
    proxy.$refs.shelfTableRef.clearSelection();
    hideTableModal();
  }else if(locationShow.value){
    form.value.locationId = '';
    form.value.locationNo = '';
    proxy.$refs.locationTableRef.clearSelection();
    hideTableModal();
  }
}
function confirmTableModalChoose(){
  if(manufacturerShow.value){
    form.value.manufacturerId = manufacturerCur.value.id;
    hideTableModal();
  }else if(brandShow.value){
    form.value.brandId = brandCur.value.id;
    hideTableModal();
  }else if(warehouseShow.value){
    form.value.warehouseId = warehouseCur.value.id;
    form.value.shelfId = '';
    form.value.locationId = '';
    hideTableModal();
  }else if(shelfShow.value){
    form.value.shelfId = shelfCur.value.id;
    form.value.locationId = '';
    hideTableModal();
  }else if(locationShow.value){
    form.value.locationId = locationCur.value.id;
    form.value.locationNo = locationCur.value.locationNo;
    hideTableModal();
  }
}
function hideTableModal(){
  if(manufacturerShow.value){
    manufacturerShow.value = false;
  }else if(brandShow.value){
    brandShow.value = false;
  }else if(warehouseShow.value){
    warehouseShow.value = false;
  }else if(shelfShow.value){
    shelfShow.value = false;
  }else if(locationShow.value){
    locationShow.value = false;
  }
}
// ------------------------------选择供货商-----------------------
const manufacturerShow = ref(false);
const manufacturerLoading = ref(false);
const manufacturerDataList = ref([]);
const manufacturerTotal = ref(0);
const manufacturerCur = ref(null);
const manufacturerParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: ''
});
// ------------------------------选择品牌-----------------------
const brandShow = ref(false);
const brandLoading = ref(false);
const brandDataList = ref([]);
const brandTotal = ref(0);
const brandCur = ref(null);
const brandParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: ''
});
// ------------------------------选择仓库---------------------------
const warehouseShow = ref(false);
const warehouseLoading = ref(false);
const warehouseDataList = ref([]);
const warehouseTotal = ref(0);
const warehouseCur = ref(null);
const warehouseParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: ''
});
// ------------------------------选择货架---------------------------
const shelfShow = ref(false);
const shelfLoading = ref(false);
const shelfDataList = ref([]);
const shelfTotal = ref(0);
const shelfCur = ref(null);
const shelfParams = ref({
  pageNum: 1,
  pageSize: 10,
  warehouseId: '',
  name: ''
});
// ------------------------------选择货位---------------------------
const locationShow = ref(false);
const locationLoading = ref(false);
const locationDataList = ref([]);
const locationTotal = ref(0);
const locationCur = ref(null);
const locationParams = ref({
  pageNum: 1,
  pageSize: 10,
  shelfId: '',
  name: ''
});
function submitNow(){
  if(submitLoading.value) return;
  proxy.$refs["formRef"].validate(valid=>{
    if(valid){
      submitLoading.value = true;
      addGoodApi(form.value).then(()=>{
        submitLoading.value = false;
        ElMessageBox.confirm(
            '新增商品成功',
            '提示',
            {
              type: 'success',
              icon: markRaw(CircleCheck),
              showCancelButton: false
            }
        ).then(()=>{
          const obj = { path: "/gasManage/stockManage/goodInfo" };
          proxy.$tab.closeOpenPage(obj);
        })
      }).catch(()=>{
        submitLoading.value = false;
      })
    }else{
      proxy.$modal.alertWarning('填写内容有误！请检查');
      submitLoading.value = false;
    }
  })
}
</script>
<style lang="scss" scoped>
@import "@/assets/styles/public.scss";
:deep(.el-input){
  .el-input__wrapper{
    background-color: $formBgSet;
    box-shadow: none;
  }
}
:deep(.el-select){
  .el-select__wrapper{
    background-color: $formBgSet;
    box-shadow: none;
  }
}
:deep(.el-textarea){
  .el-textarea__inner{
    background-color: $formBgSet;
    box-shadow: none;
  }
}
.app-container{
  width: 100%;
  background: $formBgset4;
  min-height: calc(100vh - $navBarHeight);
  .dataManageTop{
    width: calc(100% - 40px);
    height: 40px;
    margin: 0 auto;
    .itemBox{
      width: calc((100% - 10px) / 4);
      height: 100%;
      position: relative;
      background: #F7F8FA;
      margin-right: 2px;
      font-weight: 500;
      font-size: 16px;
      color: #4E5969;
      line-height: 40px;
      box-sizing: border-box;
      padding-left: 60px;
      cursor: pointer;
      .sanBox{
        width: 28px;
        height: 28px;
        background: #F7F8FA;
        transform-origin: center center;
        transform: rotate(45deg);
        position: absolute;
        top: 6px;
        right: -14px;
        z-index: 1;
      }
      .whiteSanBox{
        width: 28px;
        height: 28px;
        background: #FFFFFF;
        transform-origin: center center;
        transform: rotate(45deg);
        position: absolute;
        top: 6px;
        left: -14px;
      }
      &:first-child{
        padding-left: 16px;
      }
      &.active{
        background: var(--el-color-primary);
        color: #FFFFFF;
        .sanBox{
          background: var(--el-color-primary);
        }
      }
    }
  }
  .dataManage{
    width: 100%;
    min-width: 888px;
    min-height: $pageMinHeight;
    border-radius: 6px;
    box-sizing: border-box;
    padding: 20px 0;
    .dataManageContent{
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .normalBox{
        width: 100%;
        border: 1px solid $borderColor;
        .titleBox{
          width: 100%;
          height: 44px;
          background: $formBgset3;
          padding-left: 8px;
          box-sizing: border-box;
          .line{
            width: 4px;
            height: 16px;
            background: $formBgset2;
            margin-right: 8px;
          }
          .title{
            height: 16px;
            line-height: 16px;
            font-weight: 500;
            font-size: 16px;
            color: #1D2129;
          }
        }
        .contentBox{
          width: 100%;
          padding: 24px 0;
          .inputBox{
            width: 360px;
            height: 32px;
          }
          .textareaBox{
            width: 360px;
          }
          .selectOptionBox{
            width: 360px;
          }
          .valueBox{
            width: 360px;
            height: 32px;
            background-color: $formBgSet;
            padding: 0 10px;
            color: var(--el-text-color-regular);
            cursor: pointer;
            .value{
              flex: 1;
            }
          }
          .imgBox{
            min-width: 360px;
            background: $formBgSet;
            border-radius: 4px;
            padding: 12px;
            .notice{
              height: 20px;
              font-weight: 400;
              font-size: 12px;
              color: #86909C;
              line-height: 20px;
              margin-top: 12px;
            }
          }
        }
      }
    }
    .line{
      width: 100%;
      height: 3px;
      background-color: $formBgset4;
    }
    .dataManageBottom{
      width: 100%;
      height: 72px;
    }
  }
}
</style>